<template>
  <div>
    <!-- 评论列表 -->
    <div
      class="cmt-list"
      :class="{
        'aet-cmt-container-2': isShowInput,
        'aet-cmt-container-1': !isShowInput,
      }"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        :immediate-check="false"
        finished-text="没有更多评论了,请发表评论"
        @load="onLoad"
      >
        <!-- 评论的 Item 项 -->
        <div class="cmt-item" v-for="obj in ObjComment" :key="obj.com_id">
          <!-- 头部区域 -->
          <div class="cmt-header">
            <!-- 头部左侧 -->
            <div class="cmt-header-left">
              <img :src="obj.aut_photo" alt="" class="avatar" />
              <span class="uname">{{ obj.aut_name }}</span>
            </div>
            <!-- 头部右侧 -->
            <div class="cmt-header-right">
              <van-icon
                name="like"
                size="16"
                color="red"
                v-if="obj.is_liking === true"
                @click="loveFn(true, obj)"
              />
              <van-icon
                name="like-o"
                size="16"
                color="gray"
                v-else
                @click="loveFn(false, obj)"
              />
            </div>
          </div>
          <!-- 主体区域 -->
          <div class="cmt-body">
            {{ obj.content }}
          </div>
          <!-- 尾部区域 -->
          <div class="cmt-footer">{{ Time(obj.pubdate) }}</div>
        </div>
      </van-list>
    </div>

    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="isShowInput === false">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="onFocusFn">发表评论</div>
      <div class="icon-box">
        <van-badge>
          <!-- 消息栏 -->
          <van-icon
            name="comment-o"
            size="0.53333334rem"
            :badge="totalCount"
            @click="commentcheck"
          />
        </van-badge>
        <!-- 收藏小星星 -->
        <van-icon name="star-o" size="0.53333334rem" />
        <!-- 分享按钮 -->
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        placeholder="友善评论、理性发言、阳光心灵"
        v-fofo
        @blur="BlurFn"
        v-model.trim="comText"
      ></textarea>
      <van-button type="default" :disabled="comText ==='' " @click="submit">发布</van-button>
    </div>
  </div>
</template>

<script>
import { commentAPI, commentLikingAPI, commentUnLikingAPI ,submitAPI} from "@/api";
import { timeAgo } from "@/utils/date.js";
export default {
  name: "CommentList",
  data() {
    return {
      type: "a", //评论类型， a为对文章的评论，c表示对评论的回复
      //存储评论对象
      ObjComment: [],
      //评论的总数量
      totalCount: 0,
      isShowInput: false, //控制一开始显示的是，小的评论输入框
      last_id: null,
      //控制触底进行加载
      loading: false,
      finished: false,
      //发表评论信息存储
      comText:''
    };
  },
  async created() {
    await this.commnetget();
  },
  methods: {
    //调用工具类里面的函数处理时间
    Time: timeAgo,
    //获取评论
    async commnetget() {
      const res = await commentAPI({
        type: this.type,
        article_id: this.$route.query.art_id,
      });
      //将得到的数据存储到 ObjComment 里面
      console.log(res);
      this.ObjComment = res.data.data.results;
      // 将评论的数量长度给赋值
      this.totalCount = res.data.data.total_count;
      this.last_id = res.data.data.last_id;
    },
    //给评论点赞==>取消点赞
    async loveFn(bool, like) {
      if (bool === true) {
        //调用接口
        //取消点赞
        like.is_liking = false;
        await commentUnLikingAPI({
          commentId: like.com_id,
        });
      } else {
        //给评论点赞
        like.is_liking = true;
        await commentLikingAPI({
          commentId: like.com_id,
        });
      }
    },
    //点击之后，控制点击框的升起
    onFocusFn() {
      this.isShowInput = true;
    },
    //失去焦点之后，控制点击框降
    BlurFn() {
      //将他改为异步请求，因为点击发表按钮的时候，他会失去焦点，导致窗口关闭，发送不出去请求s
      setTimeout(()=>{
        this.isShowInput = false;
      },0)
      
    },
    //点击评论的图标，会滑动到评论区 ,滑动距离为文章的长度
    // 分为四种情况
    // 两种有评论的时候，1. 有评论文章很长  2. 有评论，文章很短  两种评论的时候 1. 无评论文章很长  2. 无评论，文章很短

    commentcheck() {
      //通过id 获取文章的高度 进行页面滚动，效果不丝滑
      // const scollHeight = document.querySelector(".article-container").scrollHeight;
      // // console.log(scollHeight);
      // window.scroll(0 ,scollHeight);

      // const scollHeight = document.querySelector(".article-container").offsetHeight;
      // // 文章卷起页面的高度
      // const now = window.scrollY;
      // // 步长每次都在变化，为页面移动的距离
      // const step = (scollHeight - now) / 10;
      // setTimeout(() => {
      //   //文章的高度
      //   if (step < 1) {
      //     window.scrollTo(0, scollHeight);
      //   }
      //   window.scrollTo(0, now + step);
      //   //调用函数，再次发生滚动
      //   this.commentcheck()
      // }, 10);

      // setInterval(() => {
      //   const scollHeight =document.querySelector(".article-container").offsetHeight;
      //   const now = window.scrollY;
      //   const step = (scollHeight -now) / 10;
      //   if (step < 1 ) return
      //   window.scrollTo(0,step)
      // }, 175);

      // console.log(111);
      // window.scrollToView(".like-box")

      //自动滑动到相应的 element 位置
      document.querySelector(".like-box").scrollIntoView({
        behavior: "smooth",
      });
    },
    //触底触发加载的函数
    async onLoad() {
      if (this.ObjComment.length > 0) {
        const res = await commentAPI({
          type: this.type,
          article_id: this.$route.query.art_id,
          offset: this.last_id,
        });
        //将得到的数据存储到 ObjComment 里面
        // console.log(res);
        this.ObjComment = [...this.ObjComment, ...res.data.data.results];
        // this.ObjComment = res.data.data
        this.last_id = res.data.data.last_id;
        
        if( res.data.data.last_id === null){
          this.finished = true
        }
        this.loading = false
       
      } else {
        //如果评论的长度 为 0 ，也就是没有评论，将 loading函数改为 false
        // 不然后面请求回来函数之后，进不去 else ，触底的时候就显示正在加载，不会触发里面的函数
        this.loading = false;
      }
    },
    async submit() {
      // console.log(this.commnetInfo1);
       const res =await submitAPI({
        article_id:this.$route.query.art_id,
        content:this.comText
      })
      this.comText = ''
      this.ObjComment.unshift(res.data.data.new_obj)
      this.totalCount = this.totalCount +1
      this.commentcheck()
      
    }
  },
};
</script>

<style scoped lang="less">
.cmt-list {
  padding: 10px;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
    }
  }
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.aet-cmt-container-1 {
  padding-bottom: 46px;
}
.aet-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
